﻿using System;
using System.Data;
using System.Linq;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Security;
using System.Web.UI;

namespace ASPNETVN.PORTAL.Components.Share
{
    public partial class Category
    {
        public static void BuildDropDownListControlForAll(ref DropDownList ddlCategory, Guid moduleID)
        {
            IList<Category> allCategories = new List<Category>();
            allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                ddlCategory.Items.Add(new ListItem(topCategory.Title, "0"));
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();
                foreach (Category childCategory in childCategories)
                {
                    ddlCategory.Items.Add(new ListItem("» " + childCategory.Title, childCategory.ID.ToString()));
                }
            }
        }

        public static void BuildCheckBoxListControl(ref CheckBoxList chkCategories, Guid moduleID)
        {
            IList<Category> allCategories = new List<Category>();

            // Administrators Role.
            if (PortalSecurity.IsInRole("1"))
            {
                allCategories = Category.SelectCollectionAll(moduleID);
            }
            else
            {
                allCategories = Category.SelectBy_UserID_ModuleID(PortalSecurity.CurrentUser.Identity.ID, moduleID);
            }
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                chkCategories.Items.Add(new ListItem(topCategory.Title, "0"));
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();
                foreach (Category childCategory in childCategories)
                {
                    chkCategories.Items.Add(new ListItem("» " + childCategory.Title, childCategory.ID.ToString()));
                }
            }
        }

        public static void BuildDropDownListControl(ref DropDownList ddlCategory, Guid moduleID)
        {
            IList<Category> allCategories = new List<Category>();
            
            // Administrators Role.
            if (PortalSecurity.IsInRole("1"))
            {
                allCategories = Category.SelectCollectionAll(moduleID);
            }
            else
            {
                allCategories = Category.SelectBy_UserID_ModuleID(PortalSecurity.CurrentUser.Identity.ID, moduleID);
            }
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                ddlCategory.Items.Add(new ListItem(topCategory.Title, "0"));
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();
                foreach (Category childCategory in childCategories)
                {
                    ddlCategory.Items.Add(new ListItem("» " + childCategory.Title, childCategory.ID.ToString()));
                }
            }
        }

        public static IList<Category> SelectBy_UserID_ModuleID(int userID, Guid moduleID)
        {
            const string spName = "[dbo].[p_SHARE_Category_SelectBy_UserID_ModuleID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID); 
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            IDataReader reader = db.ExecuteReader(dbCommand);
            IList<Category> collection = new List<Category>();
            while (reader.Read())
            {
                Category entity = new Category();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ParentID"))) entity.ParentID = reader.GetInt32(reader.GetOrdinal("ParentID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) entity.SortOrder = reader.GetInt32(reader.GetOrdinal("SortOrder"));
                if (!reader.IsDBNull(reader.GetOrdinal("RequiredApproved"))) entity.RequiredApproved = reader.GetBoolean(reader.GetOrdinal("RequiredApproved"));

                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }

        //Trangnt added
        public static void BuildDropDownListControlForAllID(ref DropDownList ddlCategory, Guid moduleID)
        {
            IList<Category> allCategories = new List<Category>();
            allCategories = Category.SelectCollectionAll(moduleID);
            IList<Category> topCategories = allCategories.Where(x => x.ParentID == 0).OrderBy(x => x.Title).ToList();

            foreach (Category topCategory in topCategories)
            {
                ddlCategory.Items.Add(new ListItem(topCategory.Title, topCategory.ID.ToString()));
                IList<Category> childCategories = allCategories.Where(x => x.ParentID == topCategory.ID).OrderBy(x => x.Title).ToList();
                foreach (Category childCategory in childCategories)
                {
                    ddlCategory.Items.Add(new ListItem("» " + childCategory.Title, childCategory.ID.ToString()));
                }
            }
        }
    }
}
